﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CNCSoft.EData;

using System.Threading;
using System.Globalization;

using Common;

namespace CNCSoft.ErpApps.TMS
{
    public partial class frmLogout : Form
    {
        public frmLogout()
        {
            // Sets the UI culture to VietNamese (VietNam) / English
            if (GlobalVariable.WSInfo.Language == "Tiếng Việt")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("vi-VN");
            else if (GlobalVariable.WSInfo.Language == "English")
                Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");

            InitializeComponent();
        }

        private void cmdLogout_Click(object sender, EventArgs e)
        {
            //Luu thong tin logout
            try
            {
                //Xoa cac record trong vong 15 phut (coi nhu ko co logout cua lan truoc day trong vong 15 phut)
                //SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString, "Delete SYS_UserLog " +
                //          "Where UserID = '" + txtUserID.Text + "' AND (datepart(mi, getdate() -  [datetime]) <15) AND ([status]) = 'O'" );

                //Tam thoi chua xu dung chuc nang XOA

                //Insert cho lan logout nay
                SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString, "insert into SYS_UserLog([UserID], [DateTime], [Status], [Note]) " +
                                                         "Values ('" + txtUserID.Text + "', getdate()  ,'O', " + "N'" + txtNote.Text + "')");

                FormMain frm = null;
                frm = (FormMain)this.Owner;
                if (frm != null)
                {
                    frm.Tag = "Exit";
                    frm.Close();
                }
                
            }
            catch (Exception ex) { MessageBox.Show("Form Logout: " + ex.Message); };
        }

        private void frmLogout_Load(object sender, EventArgs e)
        {
            try
            {
                txtUserID.Text = GlobalVariable.WSInfo.UserID;
                txtDateTime.Text = GlobalVariable.WSInfo.ServerDate.ToString();

                cmdLogout.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Form Logout: " + ex.Message);
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtNote_TextChanged(object sender, EventArgs e)
        {
            cmdLogout.Enabled = txtNote.Text.Trim() != "";
        }
    }
}
